<template>
    <div>
        <!--添加-->
        <div class="permissMannaTool">
            <el-input size="small" placeholder="请输入角色英文名" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input size="small" v-model="role.namezh" placeholder="请输入角色中文名" @keydown.enter.native="doAddRole"/>
            <el-button type="primary" size="small" icon="el-icon-plus" @click="doAddRole">添加</el-button>
        </div>
        <!--展示信息-->
        <div class="permissManaMian">
            <el-collapse accordion @change="change" v-model="activeName">
                <el-collapse-item :title="role.namezh" :key="index" :name="role.id" v-for="(role,index) in roles">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>
                            <el-button style="float: right; padding: 3px 0;color: #ff0000" icon="el-icon-delete"
                                       type="text" @click="doDeleteRole(role)"></el-button>
                        </div>
                        <div>
                            <!--树形控件-->
                            <el-tree :data="allMenus"
                                     :props="defaultProps"
                                     ref="tree"
                                     show-checkbox
                                     :key="index"
                                     :default-checked-keys="selectedMenus"
                                     node-key="id">
                            </el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
                                <el-button size="mini" type="primary" @click="doUpdate(role.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissMana",
        data() {
            return {
                role: {
                    name: '',
                    namezh: ''
                },
                roles: [],
                allMenus: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                selectedMenus: [],
                activeName:-1,
            }
        },
        mounted() {
            this.initRoles();
        },
        methods: {
            doDeleteRole(role){
                this.$confirm('此操作将永久删除[' + role.namezh + ']角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/permiss/role/' + role.id).then(resp => {
                        if (resp) {
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doAddRole(){
                if(this.role.name && this.role.namezh){
                    this.postRequest('/system/basic/permiss/role',this.role).then(resp=>{
                        if(resp){
                            this.initRoles();
                            this.role.namezh=''
                            this.role.name=''
                        }
                    })
                }else {
                    this.$message.error('所有子段不能为空')
                }
            },
            cancelUpdate(){
                this.activeName=-1;
            },
            doUpdate(rid, index) {
                let tree = this.$refs.tree[index];
                let checkedKeys = tree.getCheckedKeys();
                let url = '/system/basic/permiss/?rid=' + rid;
                checkedKeys.forEach(key => {
                    url += '&mids=' + key
                })
                this.putRequest(url).then(resp => {
                    if (resp) {
                        this.activeName=-1;
                    }
                })

            },
            change(rid) {
                if (rid) {
                    this.initAllMenus();
                    this.initSelectMenus(rid);
                }
            },
            initSelectMenus(rid) {
                this.getRequest('/system/basic/permiss/mid/' + rid).then(resp => {
                    if (resp) {
                        this.selectedMenus = resp;
                    }
                })
            },
            initAllMenus() {
                this.getRequest('/system/basic/permiss/menus').then(resp => {
                    if (resp) {
                        this.allMenus = resp;
                    }
                })
            },
            initRoles() {
                this.getRequest('/system/basic/permiss/').then(resp => {
                    if (resp) {
                        this.roles = resp;
                    }
                })
            },
        }
    }
</script>

<style scoped>
    .permissMannaTool {
        display: flex;
        justify-content: flex-start;
    }

    .permissMannaTool .el-input {
        width: 300px;
        margin-right: 8px;
    }

    .permissManaMian {
        margin-top: 10px;
        width: 700px;
    }
</style>